package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.daimajia.swipe.SwipeLayout;
import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.CustomermGroupAdapter;
import com.epfresh.supplier.entity.CustomerGroupInfo;
import com.epfresh.supplier.entity.CustomerGroupItem;
import com.epfresh.supplier.entity.CustomerInfo;
import com.epfresh.supplier.widget.BaseItemDecoration;
import com.epfresh.supplier.widget.RecyclerViewAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.epfresh.api.global.BaseActivity;
import com.epfresh.supplier.widget.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class CustomerGroupActivity extends BaseActivity implements ViewVariable, RecyclerViewAdapter.OnItemClickListener {

    private ArrayList<CustomerGroupItem> mGroupItem;
    private RecyclerView mGroupRv;
    private CustomermGroupAdapter mCustomermGroupAdapter;
    private String mAction;
    private String mEnterCustomerActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_group);
        initView();
        initData();
        initRv();
    }
    @Override
    protected View getToolBar() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initRv() {
        mGroupRv = (RecyclerView) findViewById(R.id.rv_group);
        mGroupRv.setLayoutManager(new LinearLayoutManager(this));
        mGroupRv.addItemDecoration(new BaseItemDecoration(this));
        mCustomermGroupAdapter = new CustomermGroupAdapter(null);
        mGroupRv.setAdapter(mCustomermGroupAdapter);
    }

    private void initData() {
        mAction = getIntent().getAction();
        mEnterCustomerActivity = getIntent().getStringExtra("enterCustomerActivity");
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_account_customer_groups);
        Map<String, String> map = new HashMap<>();
        map.put("merchantId", BaseApplication.getInstance().getMerchantId());//todo:
        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_account_customer_groups, new OnRequestListener<CustomerGroupInfo>() {
            @Override
            public CustomerGroupInfo jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, new TypeToken<CustomerGroupInfo>() {
                }.getType());
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onStart(Object tag) {
                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<CustomerGroupInfo> re, Object tag) {
                hideProgressDialog();
                if (null != re) {
                    mGroupItem = re.getResponseElement().getGroups();
                    mCustomermGroupAdapter.setAction(mAction);
                    mCustomermGroupAdapter.setData(mGroupItem);
                    mCustomermGroupAdapter.setOnItemClickListener(CustomerGroupActivity.this);
                }
            }
        });
    }

    private void initView() {
        findViewById(R.id.back_ll).setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        super.onClick(v);
        switch(v.getId()){
            case R.id.back_ll:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(RecyclerView parent, View view, int position, SwipeLayout swipeLayout) {
        switch (parent.getId()) {
            case R.id.rv_group:
                switch (view.getId()) {
                    case R.id.item_ll_group:
                        //选择产品  返回
                        if (CUSTOMER_INFO_ACTIVITY.equals(mAction)) {
                            Intent intent = new Intent(this, CustomerGroupContactActivity.class);
                            intent.putExtra("GroupItem", mCustomermGroupAdapter.getItem(position));
                            intent.putExtra("enterCustomerActivity", mEnterCustomerActivity);//记录是从哪个界面进入客户列表
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent();
                            intent.putExtra("GroupItem", mCustomermGroupAdapter.getItem(position));
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                        break;
                }
        }
    }
}
